<?php
namespace Account\Controller;

use Common\Controller\ApiController;
use Common\Model\ModelException;

class UserFavoriteController extends ApiController
{
    /**
     * 获取关注的人
     *
     * @param int $uid 要查询的用户ID
     */
    public function getFavorites()
    {
        $input = $this->request();
        $page = (int)filter($input['page'], 1);
        $perPage = (int)filter($input['per_page'], 10);
        $keyword = filter($input['keyword'], '');
        $user = $this->user;
        $uids = D('UserFavorite')->where(['user_id' => $user['id']])->getField('to_user_id', true);
        $count = 0;
        $users = [];
        if ($uids) {
            $where = [];
            $where['user_id'] = ['in', $uids];
            $where['nickname'] = ['like', "%{$keyword}%"];
            if (isset($input['role'])) $where['role'] = $input['role'];

            $count = D('Users')
                // ->scope('valid')  //业务混乱，先允许其被搜索出来
                ->where($where)->count();
            $users = D('Users')
                // ->scope('valid') //业务混乱，先允许其被搜索出来
                ->where($where)
                ->page($page, $perPage)
                ->select();
        }
//        else {
            // 如果没有推荐的人，返回推荐用户
//            if ($page > 1) {
//                $count = 0;
//                $users = [];
//            } else {
//                $count = 10;
//                $where = [
//                    'user_id' => ['neq', $this->user['id']],
//                ];
//                $order = [
//                    'followers_count' => 'desc',
//                ];
//                $users = D('Users')->scope('valid')
//                    ->where($where)
//                    ->order($order)
//                    ->limit(10)
//                    ->select();
//            }
//        }
        $pagination = pagination($count, $perPage, $page);
        $users = transform('User', 'pub', $users);
        $this->response($users, $pagination);
    }

    public function getFans()
    {
        $input = $this->request();
        $user = $this->user;
        $page = (int)filter($input['page'], 1);
        $perPage = (int)filter($input['per_page'], 10);
        $uids = D('UserFavorite')->where(['to_user_id' => $user['id']])->getField('user_id', true);
        $count = count($uids);
        $users = [];
        if ($uids) {
            $users = D('Users')
                ->where(['user_id' => ['in', $uids]])
                ->page($page, $perPage)
                ->select();
        }
        $pagination = pagination($count, $perPage, $page);
        $users = transform('User', 'pub', $users);
        $this->response($users, $pagination);
    }

 
    public function add()
    {
        $input = $this->request();
        $user = $this->user;
        $to = explode(',', $input['to']);
        if (in_array($user['id'], $to)) {
            $this->errorResponse(40003);
        }

        $favoriteModel = D('UserFavorite');
        try {
            $favoriteModel->addFavorites($user['id'], $to);
            $this->createdResponse();
        } catch (ModelException $e) {
            $this->errorModelException($e);
        }
    }



    public function del()
    {
        $input = $this->request();
        $to = $input['id'];
        $favorite = D('UserFavorite')->where(['user_id' => $this->user['id'], 'to_user_id' => $to])->find();
        if ($favorite) {
            D('UserFavorite')->where(['user_id' => $this->user['id'], 'to_user_id' => $to])->delete();
            D('Users')->where(['user_id' => $this->user['id']])->setDec('following_count');
            D('Users')->where(['user_id' => $to])->setDec('followers_count');
            $this->modifySuccessResponse();
        } else {
            $this->errorResponse(40004);
        }
    }

}
